﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;

namespace ABFramework
{

    /// <summary>
    /// 配置公告面板
    /// </summary>
    public class NoticeViewport : EditorWindow
    {
        static string line = "--------------------";
        static string startDate;                    //公告显示开始日期
        static string endDate;                      //公告显示结束日期
        static bool showNotice;                     //是是否显示公告信息
        static string content;                      //公告内容
        static EditorWindow win = null;


        [MenuItem("Tools/配置公告信息", false, 1)]
        public static void ShowWindow()
        {
            // 显示某个编辑器窗口。传参即是要显示的窗口类型（类名）
            win = EditorWindow.GetWindow(typeof(NoticeViewport), true, "配置公告信息");
            win.minSize = new Vector2(600, 300);
            win.maxSize = new Vector2(600, 300);

            //读取本地热更资源版本配置文件恢复显示
            string config_file = Application.dataPath + "/../" + PathTools.BUILD_AB_OUT_PATH + "/" + PathTools.NOTICE_CONFIG;
            if (File.Exists(config_file))
            {
                //热更资源版本配置文件数据对象-取到版本号
                NoticeObject vobj = JsonUtility.FromJson<NoticeObject>(File.ReadAllText(config_file));
                startDate = vobj.start_date;
                endDate = vobj.end_date;
                showNotice = vobj.show_notice;
                content = vobj.content;
                Debug.Log("本地公告信息配置恢复完成!");
            }
            else
            {
                Debug.Log("项目发布目录没找到本地公告信息配置!");
                startDate = CommonTools.GetNowTimeAndFormat("yyyy-MM-dd");
                endDate = "2099-12-31";
                showNotice = true;
                content = "公告信息内容!当开启了【显示公告选项】则会在【开始和结束日期之间】展示公告,否则公告将不会被显示出来!";
            }
        }

        /// <summary>
        /// 关闭本窗口
        /// </summary>
        public static void CloseWindow()
        {
            if (win != null) win.Close();
        }

        void OnGUI()
        {
            // 提示文本
            GUILayout.Label("提示:即使没有公告配置文件也可以,如果需要添加即可!");


            GUILayout.Label(line + line + line + line + line);
            GUILayout.Label("公告内容:");
            // 可以编辑，编辑后用同一个变量保存结果
            content = EditorGUILayout.TextArea(content, GUILayout.Height(100));

            GUILayout.Space(10);


            // 显示公告
            showNotice = EditorGUILayout.Toggle("显示公告:", showNotice);
            GUILayout.Space(10);

            //开始日期
            startDate = EditorGUILayout.TextField("开始日期:", startDate);
            //结束日期
            endDate = EditorGUILayout.TextField("结束日期:", endDate);

            //生成公告信息配置
            if (GUI.Button(new Rect(479, 170, 110, 22), "生成公告信息配置"))
            {
                CreateNoticeFile();
            }

            //删除版本配置选项
            if (GUI.Button(new Rect(360, 170, 110, 22), "删除公告信息配置"))
            {
                string config_file = Application.dataPath + "/../" + PathTools.BUILD_AB_OUT_PATH + "/" + PathTools.NOTICE_CONFIG;
                if (File.Exists(config_file)) File.Delete(config_file);
                Debug.Log("删除公告信息配置完成!");
            }

        }

        /// <summary>
        /// 创建版本文件信息
        /// </summary>
        void CreateNoticeFile()
        {
            if (!CheckDateLegal(startDate) || !CheckDateLegal(endDate))
            {
                Debug.LogError("日期格式错误!应为【年年年年-月月-日日】如:2022-03-16");
                return;
            }

            NoticeObject vobj = new NoticeObject(showNotice, content, startDate, endDate);
            string desc = JsonUtility.ToJson(vobj);

            //配置文件路径
            string out_path = Application.dataPath + "/../" + PathTools.BUILD_AB_OUT_PATH;
            string config_file = out_path + "/" + PathTools.NOTICE_CONFIG;

            //检测是文件和目录
            if (!Directory.Exists(out_path)) Directory.CreateDirectory(out_path);
            if (File.Exists(config_file)) File.Delete(config_file);

            File.Create(config_file).Dispose();

            //添加文件内容
            using (StreamWriter sw = new StreamWriter(config_file, true))
            {
                sw.WriteLine(desc);
            }

            Debug.Log("创建公告配置信息完成：" + desc);
        }
        /// <summary>
        /// 检测日期格式是否争正确
        /// </summary>
        /// <param name="date_str"></param>
        /// <returns></returns>
        bool CheckDateLegal(string date_str)
        {
            Regex regex = new Regex(@"^(\d{4})-(\d{2})-(\d{2})$");
            if (regex.IsMatch(date_str))
            {
                return true;
            }

            return false;
        }
    }

}
